//
//  EmoticonsManager.swift
//  EmoticonKeyBoard
//
//  Created by apple on 15/12/25.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit


//负责从 plist 文件中 加载表情数据
class EmoticonsManager: NSObject {

    //分组表情数组
    lazy var packages = [EmoticonPackages]()
    
    //实例化的时候记载分组表情
    override init() {
        super.init()
        loadEmoticons()
    }
    
    
    func loadEmoticons() {
        // 1.获取bundle 文件目录
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        //判断path 是否为空
        guard let filePath = path else {
            print("文件目录不存在")
            return
        }
        
//        print(filePath)
        // 2加载plist文件 转换为 字典
        let dict = NSDictionary(contentsOfFile: filePath)!
        // 3.获取字典中的数据 遍历数据 获取分组表情目录中 Info.plist文件
        if let array = dict["packages"] as? [[String : AnyObject]] {
            //遍历数据
            for item in array {
                let id = item["id"] as! String
                //获取分组表情的目录 加载 分组目录中 Info.plist
                loadGroupEmoticons(id)
            }
        }
    }
    
    
    private func loadGroupEmoticons(id: String) {
        let path = NSBundle.mainBundle().pathForResource("Info.plist", ofType: nil, inDirectory: "Emoticons.bundle/" + id)
        guard let filePath = path else {
            print("文件目录不存在")
            return
        }
        
        //一定加载到Info.plist 文件
        let dict = NSDictionary(contentsOfFile: filePath)!
//        print(dict)
        let group_name_cn = dict["group_name_cn"] as! String
        let array = dict["emoticons"] as! [[String : String]]
//        print(group_name_cn,"+++++++++++++",array)
        //实例化packages 对应的模型
        let p = EmoticonPackages(id: id, group_name_cn: group_name_cn, array: array)
        packages.append(p)
        print(p)
    }
}
